class Solution:
    def calculate(self, s: str) -> int:
        stack = []
        nowNum = 0
        hadNum = False
        for i in range(len(s)):
            if s[i] in '0123456789':
                nowNum = nowNum * 10 + int(s[i])
                hadNum = True
            elif s[i] in '-+*/':
                if hadNum:
                    stack.append(nowNum)
                    nowNum = 0
                stack.append(s[i])
                hadNum = False
        if hadNum:
            stack.append(nowNum)
        self.opt(stack)
        return stack[0]

    def opt(self, stack):
        if len(stack) <= 1:
            return
        tmp = []
        nowNum = None
        while stack:
            r = stack.pop(0)
            if r == '-' or r == '+':
                tmp.append(nowNum)
                nowNum = None
                tmp.append(r)
            elif r == '/':
                result = nowNum // stack.pop(0)
                nowNum = result
            elif r == '*':
                result = nowNum * stack.pop(0)
                nowNum = result
            else:
                nowNum = r
        if nowNum is not None:
            tmp.append(nowNum)

        result = 0
        nowNum = 0
        while tmp:
            r = tmp.pop()
            if r == '+':
                result = result + nowNum
                nowNum = 0
            elif r == '-':
                result = result + nowNum * -1
                nowNum = 0
            else:
                nowNum = r
        stack.append(result + nowNum)


if __name__ == '__main__':
    so = Solution()
    s = " 3/2 "
    r = so.calculate(s)
    print(r)
